package com.laiyima.zhongjiang.linghuilv.demo.Become;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.laiyima.zhongjiang.linghuilv.demo.R;

public class BecomeanagentActivity extends AppCompatActivity {
    private Button button;


    private static final int TIME_EXIT=3000;
    private static int isExit=0;
    //实现按两次后退才退出
    private CountDownTimer mCountDownTimer = new CountDownTimer(TIME_EXIT,1000) {
        @Override
        public void onTick(long l) {
            isExit++;

        }

        @Override
        public void onFinish() {
            isExit=0;
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK){
            exit();
            return true;
        }
        return false;
    }
    private void exit(){
        if(isExit<1){
            Toast.makeText(getApplicationContext(),"再点击一次返回退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.start();
        }else{
            isExit=0;
            Toast.makeText(getApplicationContext(),"退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.cancel();
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            System.exit(0);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_becomeanagent);
        button=findViewById(R.id.becomeagent_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent HNerintent = new Intent(BecomeanagentActivity.this, BecomeToActivity.class);
                startActivity(HNerintent);
            }
        });
    }
}
